package org.jeecg.modules.kuner.useruimestatement.service.impl;

import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.kuner.useruimestatement.entity.KrUserTimeStatement;
import org.jeecg.modules.kuner.useruimestatement.mapper.KrUserTimeStatementMapper;
import org.jeecg.modules.kuner.useruimestatement.service.IKrUserTimeStatementService;
import org.jeecg.modules.kuner.vipgoodusertime.service.IKrGoodUserTimeService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @Description: 用户次数流水记录
 * @Author: jeecg-boot
 * @Date: 2025-02-19
 * @Version: V1.0
 */
@Service
public class KrUserTimeStatementServiceImpl extends ServiceImpl<KrUserTimeStatementMapper, KrUserTimeStatement> implements IKrUserTimeStatementService {


  @Resource
  private IKrGoodUserTimeService goodUserTimeService;


  /**
   * 变动用户次数
   *
   * @param userId      用户ID
   * @param modifyTimes 变动数值
   * @param msg         变动原因
   */
  @Override
  public void modifyUserTime(String userId, BigDecimal modifyTimes, String msg) {
    if (modifyTimes.compareTo(BigDecimal.ZERO) <= 0) {
      Date userTime = getUserTime(userId);
      if (userTime.before(new Date())) {
        throw new JeecgBootException("会员到期！");
      }
    }
    KrUserTimeStatement userTimeStatement = new KrUserTimeStatement();
    userTimeStatement.setUserId(userId);
    userTimeStatement.setModifyTime(modifyTimes);
    userTimeStatement.setMsg(msg);
    save(userTimeStatement);
  }

  /**
   * 查询剩余次数
   *
   * @param userId 用户信息
   * @return 查询到的剩余次数
   */
  @Override
  public Date getUserTime(String userId) {
//        Date times = this.baseMapper.selectUserTime(userId);
//        return times == null ? BigDecimal.ZERO : times;
    return goodUserTimeService.selectExTime(userId);
  }
}
